<?php

/**
 * File Name：ContentArticlesModel.php
 * File Encoding：UTF-8
 * File New Time：2014-10-14 15:49:26
 * Author：水平凡
 * Mailbox：admin@abc3210.com
 */
class ContentArticlesModel extends BaseModel {

    public function tableName() {
        return '{{content_articles}}';
    }

    public function rules() {
        return array(
            array('title', 'required', 'message' => '标题不能为空！'),
            array('catid', 'required', 'message' => '栏目不能为空！'),
            array('ishtml,keywords,description,content,createtime,is_hot,view,like_number,admin_id,admin_username,uid,username', 'safe'),
        );
    }

    protected function beforeSave() {
        if ($this->isNewRecord) {
            //改变数据
            $this->createtime = time();
            if(GROUP_NAME == 'admin'){
                $this->admin_username = yii::app()->passport->getUserInfo();
                $this->admin_username = $this->admin_username['real_name'];
                $this->admin_id = AdminBase::$uid;
            }else{
                $this->uid = yii::app()->user->id;
                $this->username = yii::app()->user->username;
            }
        }
        return true;
    }

    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * 根据ID获取文章内容
     * @param type $id
     * @return type
     */
    public function getArticle($id){
        return $this->findByPk($id);
    }

    public function createSearchCriteria(){

        $criteria=new CDbCriteria;

        $criteria->compare('content',$this->content,true);
        $criteria->compare('title',$this->title,true);
        $criteria->compare('description',$this->description,true);

        return  $criteria;
    }

}
